#include <cstdio>
#include <cstring>

using namespace std;

const int maxn=50;

struct Edge {
    int u, v, w;
} e[maxn*maxn+1];
int s, m;

int getAns() {
    static int out[maxn+1], nex[maxn+1], id[maxn+1], vis[maxn+1];
    int ans=0;
    for (; true;) {
        memset(out, 127, sizeof out);
        for (int i=1; i<=m; i++) {
            if (out[e[i].u]>e[i].w) {
                out[e[i].u] = e[i].w, nex[e[i].u] = e[i].v;
            }
        }

        for (int i=1; i<=s; i++) ans+=out[i];

        int col=0;
        memset(id, 0, sizeof id);
        memset(vis, 0, sizeof vis);
        for (int i=1; i<=s; i++) {
            int v=i;
            for (; !vis[v] && !id[v] && v; v=nex[v]) {
                vis[v] = i;
            }
            if (vis[v]==i && v) {
                id[v] = ++col;
                for (int t=nex[v]; t!=v; t=nex[t]) id[t] = col;
            }
        }
        if (!col) return ans;

        for (int i=1; i<=s; i++) {
            if (!id[i]) id[i]=++col;
        }

        int cnt=0;
        for (int i=1; i<=m; i++) {
            int a=id[e[i].u], b=id[e[i].v], c=e[i].w-out[e[i].u];
            if (a!=b) {
                cnt++;
                e[cnt] = Edge{a, b, c};
            }
        }
        s = col;
        m = cnt;
    }
}

int main() {
    freopen("pl.in", "r", stdin);
    freopen("pl.out", "w", stdout);

    static int a[maxn+1], b[maxn+1], p[maxn+1];
    int n, ans;
    scanf("%d", &n);
    for (int i=1; i<=n; i++) scanf("%d", a+i);
    for (int i=1; i<=n; i++) scanf("%d", b+i);

    s = n;
    m = 0;
    for (int i=1; i<=n; i++) {
        for (int j=1; j<=n; j++) {
            if (i==j) e[++m] = Edge{i, 0, a[i]^b[i]};
            else e[++m] = Edge{i, j, a[i]^b[j]};
        }
    }
    ans=getAns();

    static bool book[maxn+1];
    for (int i=1; i<=n; i++) {
        for (int o=1; o<=n; o++) {
            if (!book[o]) {
                s = n;
                m = 0;
                p[i] = o;
                book[o] = true;
                for (int j=1; j<=i; j++) {
                    e[++m] = Edge{p[j], 0, a[p[j]]^b[p[j]]};
                    for (int k=1; k<j; k++) {
                        e[++m] = Edge{p[j], p[k], a[p[j]]^b[p[k]]};
                    }
                }
                for (int j=1; j<=n; j++) {
                    if (!book[j]) {
                        for (int k=1; k<=n; k++) {
                            if (j==k) e[++m] = Edge{j, 0, a[j]^b[j]};
                            else e[++m] = Edge{j, k, a[j]^b[k]};
                        }
                    }
                }
                if (getAns()==ans) break;
                book[o] = false;
            }
        }
    }

    printf("%d\n", ans);
    for (int i=1; i<=n; i++) printf("%d ", p[i]);
    printf("\n");

    fclose(stdin);
    fclose(stdout);
    return 0;
}
